package com.test.mongo.service;

import com.test.mongo.repository.CustomerRepository;
import com.test.mongo.vo.Customer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CustomerService implements CommandLineRunner {
    @Autowired
    private CustomerRepository repository;

    @Override
    public void run(String... args) throws Exception {
        this.repository.deleteAll();

        // save a couple of customers
        this.repository.save(new Customer("Alice", "Smith"));
        this.repository.save(new Customer("Bob", "Smith"));

        // fetch all customers
        log.debug("Customers found with findAll():");
        log.debug("-------------------------------");
        for (Customer customer : this.repository.findAll()) {
            log.debug(customer.toString());
        }
        log.debug("测试插入数据");

        // fetch an individual customer
        log.debug("Customer found with findByFirstName('Alice'):");
        log.debug("--------------------------------");
        log.debug(this.repository.findByFirstName("Alice").toString());

        log.debug("Customers found with findByLastName('Smith'):");
        log.debug("--------------------------------");
        for (Customer customer : this.repository.findByLastName("Smith")) {
            log.debug(customer.toString());
        }
    }
}
